﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;


using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsMembershipSettingsTableAdapters;
using ConstructionBidOnline.Common;


namespace ConstructionBidOnline.Biz
{
   public class ContractJobFeesSettings:BOBase
    {
       #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
       protected ContractJobFeesSettings() { }

        #endregion

       #region Public Properties

       #region Instance Properties

       /// <summary>
       /// PKID of the ContractJobFeesSettings.
       /// </summary>
       public int ContractJobFeesID
       {
           get { return _contractjobfeesID; }
           set
           {
               if (_contractjobfeesID != value)
               {
                   if (OnPropertyChanging("ContractJobFeesID", _contractjobfeesID, value))
                   {
                       int oldValue = _contractjobfeesID;
                       _contractjobfeesID = value;

                       OnPropertyChanged("ContractJobFeesID", oldValue, value);
                   }
               }
           }
       }  private int _contractjobfeesID = -1;






       /// <summary>
       /// Fees Range  Value.
       /// </summary>
       /// 

       public string FeesRange
       {
           get { return _feesrange; }
           set
           {
               if (_feesrange != value)
               {
                   if (OnPropertyChanging("FeesRange", _feesrange, value))
                   {
                       string oldValue = _feesrange;
                       _feesrange = value;
                       OnPropertyChanged("FeesRange", oldValue, value);
                   }
               }
           }
       }  private string _feesrange = null;











       /// <summary>
       /// Contract Job Fees  Value.
       /// </summary>
       /// 

       public decimal ContractJobFees
       {
           get { return _contractjobfees; }
           set
           {
               if (_contractjobfees != value)
               {
                   if (OnPropertyChanging("ContractJobFees", _contractjobfees, value))
                   {
                       decimal oldValue = _contractjobfees;
                       _contractjobfees = value;
                       OnPropertyChanged("ContractJobFees", oldValue, value);
                   }
               }
           }
       }  private decimal _contractjobfees = 0;


       #endregion

       #endregion

       #region Public Methods

       #region CreateNew

       /// <summary>
       /// Creates a new, empty ContractJobFeesSettings object.
       /// </summary>
       public static ContractJobFeesSettings CreateNew()
       {
           ContractJobFeesSettings obj = new ContractJobFeesSettings();

           // Switch state to Added prior to initializing property values.
           obj.ObjState = ObjectState.Added;

           //obj.ValidUpTo = DateTime.Now;

           return (obj);
       }


       [MethodImpl(MethodImplOptions.Synchronized)]
       public static ContractJobFeesSettings CreateNew(ContractJobFeesSettingsCollection collection)
       {
           ContractJobFeesSettings obj = CreateNew();

           // Get next/unique ID for the new object.
           int minVal = 0;
           foreach (ContractJobFeesSettings contractjobfeessettings in collection)
           {
               if (contractjobfeessettings.ContractJobFeesID < minVal)
                   minVal = contractjobfeessettings.ContractJobFeesID;
           }
           obj.ContractJobFeesID = --minVal;
           collection.Add(obj);

           return (obj);
       }

       #endregion

       #region GetAllContractJobFeesSettings

       /// <summary>
       /// Returns a collection of all ContractJobFeesSettings.
       /// </summary>
       public static ContractJobFeesSettingsCollection GetAllContractJobFeesSettings()
       {
           ContractJobFeesTableAdapter adapter = new ContractJobFeesTableAdapter();
           adapter.Connection = AppSettings.GetDbConn();

           dsMembershipSettings.ContractJobFeesDataTable tbl = adapter.GetData(null);

           ContractJobFeesSettingsCollection list = new ContractJobFeesSettingsCollection();

           if (tbl.Rows.Count > 0)
           {
               foreach (dsMembershipSettings.ContractJobFeesRow row in tbl.Rows)
               {
                   ContractJobFeesSettings contractjobfeessettings = new ContractJobFeesSettings();
                   RelationalToObject(contractjobfeessettings, row);
                   list.Add(contractjobfeessettings);
               }
           }

           return (list);
       }

       #endregion

       #region GetContractJobFeesSettingsByID

       /// <summary>
       /// Returns an ContractJobFeesSettings object for the specified 'ContractJobFeesID' or null if no ContractJobFeesSettings with the specified parameter(s) exists.
       /// </summary>
       public static ContractJobFeesSettings GetContractJobFeesSettingsByID(int contractjobfeesID)
       {
           ContractJobFeesTableAdapter adapter = new ContractJobFeesTableAdapter();
           adapter.Connection = AppSettings.GetDbConn();
           dsMembershipSettings.ContractJobFeesDataTable tbl = adapter.GetData(contractjobfeesID);

           if (tbl.Rows.Count == 1)
           {
               ContractJobFeesSettings ms = new ContractJobFeesSettings();
               RelationalToObject(ms, tbl.Rows[0]);
               return (ms);
           }
           else if (tbl.Rows.Count > 1)
               throw new ApplicationException("Database integrity error: Specified 'Settings' returned more than one User record.");

           return (null);
       }

       #endregion

       #region GetContractJobFeesSettingsByFeesRange

       /// <summary>
       /// Returns an ContractJobFeesSettings object for the specified 'FeesRange' or null if no ContractJobFeesSettings with the specified parameter(s) exists.
       /// </summary>
       public static ContractJobFeesSettings GetContractJobFeesSettingsByFeesRange(string feesrange)
       {
           ContractJobFeesTableAdapter adapter = new ContractJobFeesTableAdapter();
           adapter.Connection = AppSettings.GetDbConn();
           dsMembershipSettings.ContractJobFeesDataTable tbl = adapter.GetDataByFeesRange(feesrange);

           if (tbl.Rows.Count == 1)
           {
               ContractJobFeesSettings ms = new ContractJobFeesSettings();
               RelationalToObject(ms, tbl.Rows[0]);
               return (ms);
           }
           else if (tbl.Rows.Count > 1)
               throw new ApplicationException("Database integrity error: Specified 'Settings' returned more than one User record.");

           return (null);
       }

       #endregion

       #region Update

       /// <summary>
       /// Updates the specified ContractJobFeesSettings.
       /// </summary>
       public static void Update(ContractJobFeesSettings obj)
       {
           bool updatecontractjobfeessettings = false;

           // Do not update invalid objects.
           if (!obj.Validate())
           {
               StringBuilder sb = new StringBuilder(2048);
               sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
               foreach (ValidationError err in obj.Errors)
                   sb.Append("\n* " + err.Element + ": " + err.Message);

               throw new InvalidObjectException(sb.ToString());
           }
           else
           {
               updatecontractjobfeessettings = (obj.ObjState != ObjectState.Unchanged);

               if (!updatecontractjobfeessettings)
                   return;
           }

           ObjectState originalObjectState = obj.ObjState;

           dsMembershipSettings.ContractJobFeesDataTable tbl = null;
           if (updatecontractjobfeessettings)
           {

               ContractJobFeesTableAdapter adapter = new ContractJobFeesTableAdapter();
               adapter.Connection = AppSettings.GetDbConn();


               tbl = new dsMembershipSettings.ContractJobFeesDataTable();
               dsMembershipSettings.ContractJobFeesRow row = tbl.NewContractJobFeesRow();
               ObjectToRelational(obj, tbl, row);

               adapter.Update(tbl);

               if (originalObjectState == ObjectState.Deleted)
                   obj = null;
               else RelationalToObject(obj, tbl.Rows[0]);
           }
       }

       #endregion

       #region Validate

       /// <summary>
       /// Validates the object based on its properties at the time of the call; updates the Errors property.
       /// </summary>
       public override bool Validate()
       {
           Errors.Clear();

           if (ObjState == ObjectState.Unchanged)
           {
               // By definition, unchanged objects are valid.
               return (true);
           }
           else if (ObjState == ObjectState.Uninitialized)
           {
               Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

               // No need to proceed past this error.
               return (false);
           }
           else if ((ObjState == ObjectState.Deleted) && (ContractJobFeesID <= 0))
           {
               // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
               Errors.Add(new ValidationError("ContractJobFeesID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

               // No need to proceed past this error.
               return (false);
           }
           else  // ObjState == Added | Modified
           {

               // MembershipFees is required.
               if ((ContractJobFees < 0))
               {
                   Errors.Add(new ValidationError("ContractJobFeesSettings", "ContractJobFees is required.", ValidationError.Severity.Error));
               }
               else if (ObjState == ObjectState.Added)
               {

               }

           }

           return (Errors.Count == 0);
       }

       #endregion

       #region ToString

       /// <summary>
       /// User-friendly string represetation of this object.
       /// </summary>
       public override string ToString()
       {
           return (String.Format("ContractJobFeesSettings: MSID={0}, ContractJobFees={1}, {2}", ContractJobFeesID, ContractJobFees, FeesRange));
       }

       #endregion


       #endregion

    }


   /// <summary>
   /// Collection of 0 or more SubAdmin objects.
   /// </summary>

   public class ContractJobFeesSettingsCollection : List<ContractJobFeesSettings>
   {

       #region Types

       public class UserComparer : IComparer<ContractJobFeesSettings>
       {
           public int Compare(ContractJobFeesSettings user1, ContractJobFeesSettings user2)
           {
               return (String.Compare(user1.FeesRange, user2.FeesRange));
           }
       }

       #endregion

       #region Public Methods

       #region FindByContractJobFeesSettingsID

       public ContractJobFeesSettings FindByContractJobFeesSettingsID(int contractjobfeesID)
       {
           for (int ndx = 0; ndx < this.Count; ndx++)
               if (this[ndx].ContractJobFeesID == contractjobfeesID)
                   return (this[ndx]);

           return (null);
       }

       #endregion

       #region FindByContractJobFeesRange

       public ContractJobFeesSettings FindByContractJobFeesRange(string feesrange)
       {
           for (int ndx = 0; ndx < this.Count; ndx++)
               if (this[ndx].FeesRange == feesrange)
                   return (this[ndx]);

           return (null);
       }

       #endregion

       #endregion

   }
}
